home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / DistUpgrade / apt-autoinst-fixup.py next >
Text File  |  2009-11-02  |  3KB  |  72 lines

  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import os
  5.  
  6. import warnings
  7. warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning)
  8. import apt
  9. import apt_pkg
  10. import logging
  11.  
  12. logging.basicConfig(level=logging.DEBUG,
  13.                     filename="/var/log/dist-upgrade/apt-autoinst-fixup.log",
  14.                     format='%(asctime)s %(levelname)s %(message)s',
  15.                     filemode='w')
  16.  
  17. cache = apt.Cache()
  18.  
  19. min_version = "0.6.20ubuntu13"
  20. if apt_pkg.VersionCompare(cache["python-apt"].installedVersion, min_version) < 0:
  21.     logging.error("Need at least python-apt version %s " % min_version)
  22.     sys.exit(1)
  23.  
  24.  
  25. # figure out what needs fixup
  26. logging.debug("Starting to check for auto-install states that need fixup")
  27. need_fixup = set()
  28.  
  29. # work around #105663
  30. need_fixup.add("mdadm")
  31.  
  32. for pkg in cache:
  33.     if pkg.isInstalled and pkg.section == "metapackages":
  34.         logging.debug("Found installed meta-pkg: '%s' " % pkg.name)
  35.         dependsList = pkg._pkg.CurrentVer.DependsList
  36.         for t in ["Depends","PreDepends","Recommends"]:
  37.             if dependsList.has_key(t):
  38.                 for depOr in dependsList[t]:
  39.                     for dep in depOr:
  40.                         depname = dep.TargetPkg.Name
  41.                         if (cache[depname].isInstalled and
  42.                             cache._depcache.IsAutoInstalled(cache[depname]._pkg)):
  43.                             logging.info("Removed auto-flag from package '%s'" % depname)
  44.                             need_fixup.add(depname)
  45.         
  46. # now go through the tagfile and actually fix it up
  47. if len(need_fixup) > 0:
  48.     # action is set to zero (reset the auto-flag)
  49.     action = 0
  50.     STATE_FILE = apt_pkg.Config.FindDir("Dir::State") + "extended_states"
  51.     # open the statefile
  52.     if os.path.exists(STATE_FILE):
  53.         tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
  54.         outfile = open(STATE_FILE+".tmp","w")
  55.         while tagfile.Step():
  56.             pkgname = tagfile.Section.get("Package")
  57.             autoInst = tagfile.Section.get("Auto-Installed")
  58.             if pkgname in need_fixup:
  59.                 newsec = apt_pkg.RewriteSection(tagfile.Section,
  60.                                                 [],
  61.                                        [ ("Auto-Installed",str(action)) ]
  62.                                        )
  63.                 outfile.write(newsec+"\n")
  64.             else:
  65.                 outfile.write(str(tagfile.Section)+"\n")
  66.         os.rename(STATE_FILE, STATE_FILE+".fixup-save")
  67.         os.rename(outfile.name, STATE_FILE)
  68.         os.chmod(STATE_FILE, 0644)
  69.  
  70.  
  71.  
  72.